package com.example.pay_service.controller;

import com.example.pay_service.return_data.Mall_Data;
import com.example.pay_service.service.ShopcartService;
import com.example.pay_service.service.ShoppingService;
import com.example.pay_service.vo.Commodity;
import com.example.pay_service.vo.Shopping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.xml.crypto.Data;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @title: Shopping
 * @projectName mall
 * @description: TODO
 * @author TLJ
 * @date 2021/12/1615:43
 */
@RestController
@RequestMapping("pay")
public class ShoppingController {

    @Autowired
    ShoppingService shoppingService;

    @Autowired
    ShopcartService shopcartService;

    @RequestMapping("/showshopping")
    public Mall_Data showshopping(@RequestParam("user_id") int user_id){
        List<Shopping> shoppings = shoppingService.showshopping(user_id);

        if (shoppings.size()>0){
            Mall_Data mall_data = new Mall_Data();
            mall_data.setcode(1);
            mall_data.seterror("查询购物车成功");
            mall_data.setdata(shoppings);
            return mall_data;
        }
        Mall_Data mall_data = new Mall_Data();
        mall_data.setcode(0);
        mall_data.seterror("您还未添加购物车");
        mall_data.setdata("");
        return mall_data;
    }

    @RequestMapping("/query_cart")
    public Mall_Data show(@RequestParam("cart_id") int cart_id){
        System.out.println(cart_id);
        Shopping shoppings = shoppingService.query_cart(cart_id);

        if (shoppings!=null){
            Mall_Data mall_data = new Mall_Data();
            mall_data.setcode(1);
            mall_data.seterror("查询购物车成功");
            mall_data.setdata(shoppings);
            return mall_data;
        }
        Mall_Data mall_data = new Mall_Data();
        mall_data.setcode(0);
        mall_data.seterror("查询购物车失败");
        mall_data.setdata("");
        return mall_data;
    }

    @RequestMapping("/addshopping")
    public Mall_Data addshopping(Shopping shopping){

        shopping.setShopping_Cart_create(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        shopping.setMember_welfare_update(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        System.out.println(shopping.getCommodity_nametop());
        int stuster = shoppingService.addshopping(shopping);
        System.out.println(shopping.getCommodity_nametop());
        if (stuster>0){
            System.out.println(1111);
            Mall_Data mall_data = new Mall_Data();
            mall_data.setcode(1);
            mall_data.seterror("加入购物车成功");
            mall_data.setdata(shopping);
            return mall_data;
        }
        Mall_Data mall_data = new Mall_Data();
        mall_data.setcode(0);
        mall_data.seterror("加入购物车失败");
        mall_data.setdata("");
        return mall_data;

    }

    @RequestMapping("/shoppingdel")
    public Mall_Data shoppingdel(@RequestParam("cart_id") int cart_id){
        int del =  shoppingService.delshopping(cart_id);
        if (del>0){
            Mall_Data mall_data = new Mall_Data();
            mall_data.setcode(1);
            mall_data.seterror("删除成功");
            mall_data.setdata(del);
            return mall_data;

        }
        Mall_Data mall_data = new Mall_Data();
        mall_data.setcode(0);
        mall_data.seterror("删除失败");
        mall_data.setdata("");
        return mall_data;

    }

    @RequestMapping("/showshoppingsize")
    public Mall_Data showshoppingsize(@RequestParam("user_id") int user_id){
        int size= showshopping(user_id).size();
        Mall_Data mall_data = new Mall_Data();
        if (size>0){

            mall_data.setcode(1);
            mall_data.seterror("查询购物车成功");
            mall_data.setdata(size);
            return mall_data;
        }

        mall_data.setcode(0);
        mall_data.seterror("您还未添加购物车");
        mall_data.setdata("");
        return mall_data;
    }

}
